﻿using Inovout.Component;
using Inovout.Data.Domain;
using Inovout.Data.Repository;
using System.Collections.Generic;
using System.Linq;
using System;

namespace Inovout.Data.Service
{
    /// <summary>
    /// RIepositoryService的通用实现类
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public class RepositoryService<TEntity> : RepositoryService, IRepositoryService<TEntity>, IRepositoryForControllerService<TEntity>
    {
        protected IRepository<TEntity> Repository
        {
            get { return repository as IRepository<TEntity>; }
        }
        /// <summary>
        /// 新建模型对象，但没有保存至数据库。通常用于Controller中的New的Action。
        /// </summary>
        /// <returns></returns>
        public virtual TEntity New()
        {
            //  return new TEntity();
            return ComponentRegistry.Resolve<TEntity>();
        }

        public virtual void Save(TEntity entity)
        {
            DataObject dataObject = entity as DataObject;
            if (dataObject.IsTransient)
            {
                Create(entity);
            }
            else
            {
                Update(entity);
            }
        }

        public virtual void Create(TEntity entity)
        {
            Repository.Save(entity);
        }
        public virtual void Update(TEntity entity)
        {
            Repository.Save(entity);
        }
        /// <summary>
        /// 批量插入，Id生成必须为非数据库生成，如：Guid
        /// </summary>
        /// <param name="entities">模型对象集合</param>
        public void BulkInsert(IEnumerable<TEntity> entities)
        {
            this.Repository.BulkInsert(entities);
        }
        /// <summary>
        /// 批量更新，Id生成必须为非数据库生成，如：Guid，且不能为乐观锁，如：Version
        /// </summary>
        /// <param name="entities">模型对象集合</param>
        public void BulkUpdate(IEnumerable<TEntity> entities)
        {
            this.Repository.BulkUpdate(entities);
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="entities">模型对象集合</param>
        public void BulkDelete(IEnumerable<TEntity> entities)
        {
            this.Repository.BulkDelete(entities);
        }

        /// <summary>
        /// 删除实体对象
        /// </summary>
        /// <param name="entity">实体对象</param> 
        public virtual void Delete(TEntity entity)
        {
            Repository.Delete(entity);
        }
        /// <summary>
        /// 通过主键查找模型
        /// </summary>
        /// <typeparam name="TEntity">模型类型</typeparam>
        /// <param name="id">主键</param>
        /// <returns>实体对象实例</returns>
        public virtual TEntity FindById(object id)
        {
            return Repository.FindById(id);
        }

        /// <summary>
        /// 重新回棋模型，在多线程开发时使用，用于切换不同线程的Session对象实例
        /// </summary>
        /// <param name="model">模型对象实例</param>
         public virtual void Load(TEntity entity)
        {
            Repository.Load(entity);
        }
        /// <summary>
        /// 基于NHibernate的IQueryable对象实例。有状态的Session。
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="queryCriteria">查询规则对象实例</param>
        /// <returns>有状态的IQueryable对象实例</returns>
        public virtual IQueryable<TEntity> Query(QueryCriteria queryCriteria)
        {
            return Repository.Query(queryCriteria);
        }

        /// <summary>
        /// 基于NHibernate的IQueryable对象实例。有状态的Session。
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="queryCriteria">查询规则对象实例</param>
        /// <returns>无状态IQueryable对象实例</returns>
        public IQueryable<TEntity> StatelessQuery(QueryCriteria queryCriteria)
        {
            return Repository.StatelessQuery(queryCriteria);
        }
        /// <summary>
        /// 基于NHibernate的IQueryable对象实例。有状态的Session。
        /// </summary>
        /// <returns>有状态IQueryable对象实例</returns>
        public virtual IQueryable<TEntity> Query()
        {
            return Repository.Query();
        }
        /// <summary>
        /// 基于NHibernate的IQueryable对象实例。无状态的Session。
        /// </summary>
        /// <returns>无状态的IQueryable对象实例</returns>
        public virtual IQueryable<TEntity> StatelessQuery()
        {
            return Repository.StatelessQuery();
        }
        /// <summary>
        /// 查询所有的模型集合
        /// </summary>
        /// <returns>模型对象集合</returns>
        public IEnumerable<TEntity> FindAll()
        {
            return Repository.FindAll();
        }

    }
}
